const utils = require('./lib/utils')
const axios = require('axios');
const moment = require('moment');

async function pushMo() {
    log.info('mo push task start ...')
    while (true) {
        let cusList = await models.customer.find({mo_url: {$ne: ''}})
        for (let i = 0; i < 120; i++) {
            for (let c of cusList) {
                let q = {cus_id: c.account, try_times: {$lt: 4}, push_status: {$ne: 1}, lock: ''};
                let lock = i + c.account + new Date().getTime();
                let update = {
                    lock,
                    type: 1
                }
                let rows = await models.customerMo.updateMany(q, {$set: update}).limit(10);

                if (rows.modifiedCount > 0) {
                    let results = await models.customerMo.find({lock});
                    
                    if (c.push_type === 'wz') {
                        //微众上行推送逻辑
                        let itemstamp = new moment().format('YYYY-MM-DD HH:mm:ss');
                        const delivers = results.map(r => {
                            return {
                                content : r.content,
                                ext : r.ext,
                                mobile : r.mobile,
                                time : r.mo_date,
                                appId : r.cus_id
                            }
                        });

                        reqData = {
                            itemstamp,
                            delivers
                        }
                        try {
                            let resp = await axios.post(c.mo_url, reqData);
                            console.log(resp.data)
                            if (resp.data.match(/ok/i)) {
                                let sucR = await models.customerMo.updateMany({lock}, {
                                    $set: {push_status: 1, push_time: new Date()},
                                    $inc: {try_times: 1}
                                });
                                log.info(c.account + ' mo push suc:' + sucR.modifiedCount,c.mo_url, reqData)
                            } else {
                                throw resp.data
                            }
                        } catch (err) {
                            let errR = await models.customerMo.updateMany({lock}, {
                                $set: {
                                    lock : '',
                                    push_status: 1,
                                    push_time: new Date()
                                }, $inc: {try_times: 1}
                            });
                            log.error(c.account + ' mo push ERR:' + errR.modifiedCount,c.mo_url, reqData, err.message)
                        }

                    }else{
                        results = results.map(r => {
                            return `${r.mobile},${r.ext},${r.content},${r.date},,,`
                        });
                        let moStr = results.join(';')
                        try {
                            let resp = await axios.post(c.mo_url, {mo: moStr}, {headers: {"Content-Type": "application/x-www-form-urlencoded"}})
                            if (resp.data === 'success') {
                                let sucR = await models.customerMo.updateMany({lock}, {
                                    $set: {push_status: 1, push_time: new Date()},
                                    $inc: {try_times: 1}
                                });
                                log.info(c.account + ' mo push suc:' + sucR.modifiedCount, moStr)
                            } else {
                                throw resp.data
                            }
                        } catch (err) {
                            let errR = await models.customerMo.updateMany({lock}, {
                                $set: {
                                    lock : '',
                                    push_status: 1,
                                    push_time: new Date()
                                }, $inc: {try_times: 1}
                            });
                            log.error(c.account + ' mo push ERR:' + errR.modifiedCount, moStr)
                        }
                    }
                }
            }
            await utils.sleep(1000);
        }
    }
    log.info('mo push task stop ...')
}

pushMo();
